package com.lxb.blog.web;

import com.lxb.blog.service.BlogService;
import com.lxb.blog.service.TypeService;
import com.lxb.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.event.MouseListener;

@Controller
@RequestMapping("/types")
public class TypeShowController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    @GetMapping
    public String typeShow(Model model,
                           @PageableDefault(size = 1,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                   Pageable pageable)
    {
        model.addAttribute("types",typeService.listTypeTop(100));
        model.addAttribute("page",blogService.listBlog(pageable));
        model.addAttribute("activeTypeId",null);
        return "types";
    }
    @GetMapping("/{id}")
    public String types(Model model,
                        @PageableDefault(size = 2,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
            @PathVariable Integer id){

        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);
        model.addAttribute("types",typeService.listTypeTop(100));
        model.addAttribute("page",blogService.listBlog(pageable,blogQuery));
        model.addAttribute("activeTypeId",id);
        return "types";
    }

}
